package com.weibo.host.severlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.weibo.common.CommonUtil;
import com.weibo.common.ValidateUserServlet;
import com.weibo.host.service.HostService;
import com.weibo.host.service.imp.HostServiceImp;
import com.weibo.user.User;

public class MenuDispatcher extends ValidateUserServlet{
	private static final Logger logger = Logger.getLogger(MenuDispatcher.class);
	private static HostService hoserService = new HostServiceImp();
	@Override
	public void doService(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		Object obj = request.getParameter("type");
		String parm = CommonUtil.parseUrlZH(obj);
		SearchParm searchParm = HostUtil.getSearchParm(request);
		List list = null;
		int TotalPageNum = 0;
		if(!parm.equals("")){
			request.setAttribute("currentmenu", "查看微博主");
			request.setAttribute("currentmenuitem", parm);
			try {
				HostService hostService = new HostServiceImp();
				String currentPage = request.getParameter("currentPage");
				if(currentPage==null||"".equals(currentPage)){
					currentPage = "1";
				}
				User user = CommonUtil.getLoginUser(request);
				/*if(parm.equals("查看微博博主")){
				}else if(parm.equals("红人博主")){
				}else if(parm.equals("名人博主")){
				}else if(parm.equals("热门微博主")){
				}else if(parm.equals("重点推荐")){
				}else if(parm.equals("查看收藏夹")){
				}else if(parm.equals("查看历史")){
				}else if(parm.equals("查看黑名单")){
				}*/
				//获取游客列表
				//list = hostService.getHongRenList(Integer.parseInt(currentPage)-1);
				list = HostUtil.getListByParm(searchParm, hostService, parm, Integer.parseInt(currentPage)-1,user);
				//获取游客列表的总条目数
				//TotalPageNum = CommonUtil.getTotalPageNum(hostService.getHongRenSize());
				TotalPageNum = CommonUtil.getTotalPageNum(HostUtil.getTotalPageNumByParm(searchParm, hostService, parm,user));
				
				request.setAttribute("hostList", list);
				request.setAttribute("searchParm", searchParm);
				request.setAttribute("currentPage", currentPage);
				request.setAttribute("totalPageNum", TotalPageNum);
				request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block 
				e.printStackTrace();
				logger.error(e);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				logger.error(e);
			}
		}
	}
	/**
	 * 查看微博博主信息
	 * @param request
	 * @param response
	 */
	public void initPage(HttpServletRequest request,HttpServletResponse response){ 
		request.setAttribute("currentmenu", "查看微博主");
		request.setAttribute("currentmenuitem", "查看微博博主");
		try {
			HostService hostService = new HostServiceImp();
			String currentPage = request.getParameter("currentPage");
			if(currentPage==null||"".equals(currentPage)){
				currentPage = "1";
			}
			
			//获取游客列表
			List list = hostService.getHostList_ForVisitor(Integer.parseInt(currentPage)-1);
			//获取游客列表的总条目数
			int TotalPageNum = CommonUtil.getTotalPageNum(hostService.getHostTotalSize());
			
			request.setAttribute("hostList", list);
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("totalPageNum", TotalPageNum);
			request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
			logger.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
	}
	/**
	 * 红人列表
	 * @param request
	 * @param response
	 */
	public void HongRenList(HttpServletRequest request,
			HttpServletResponse response){
		//hoserService.getHongRenList(currentPage)
		request.setAttribute("currentmenu", "查看微博主");
		request.setAttribute("currentmenuitem", "红人博主");
		try {
			HostService hostService = new HostServiceImp();
			String currentPage = request.getParameter("currentPage");
			if(currentPage==null||"".equals(currentPage)){
				currentPage = "1";
			}
			//获取游客列表
			List list = hostService.getHongRenList(Integer.parseInt(currentPage)-1);
			request.setAttribute("hostList", list);
			
			//获取游客列表的总条目数
			int TotalPageNum = CommonUtil.getTotalPageNum(hostService.getHongRenSize());
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("totalPageNum", TotalPageNum);
			request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
			logger.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
	}
	/**
	 * 名人列表
	 * @param request
	 * @param response
	 */
	public void MingRenList(HttpServletRequest request,
			HttpServletResponse response){
		request.setAttribute("currentmenu", "查看微博主");
		request.setAttribute("currentmenuitem", "名人博主");
		try {
			HostService hostService = new HostServiceImp();
			String currentPage = request.getParameter("currentPage");
			if(currentPage==null||"".equals(currentPage)){
				currentPage = "1";
			}
			
			//获取游客列表
			List list = hostService.getMingRenList(Integer.parseInt(currentPage)-1);
			//获取游客列表的总条目数
			int TotalPageNum = CommonUtil.getTotalPageNum(hostService.getMingRenSize());
			
			request.setAttribute("hostList", list);
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("totalPageNum", TotalPageNum);
			request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
			logger.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	/**
	 * 热门博主
	 * @param request
	 * @param response
	 */
	public void ReMenList(HttpServletRequest request,
			HttpServletResponse response){
		request.setAttribute("currentmenu", "查看微博主");
		request.setAttribute("currentmenuitem", "热门微博主");
		try {
			HostService hostService = new HostServiceImp();
			String currentPage = request.getParameter("currentPage");
			if(currentPage==null||"".equals(currentPage)){
				currentPage = "1";
			}
			//获取游客列表
			List list = hostService.getReMenList(Integer.parseInt(currentPage)-1);
			request.setAttribute("hostList", list);
			
			//获取游客列表的总条目数
			int TotalPageNum = CommonUtil.getTotalPageNum(hostService.getReMenSize());
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("totalPageNum", TotalPageNum);
			request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
			logger.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	/**
	 * 重点推荐
	 * @param request
	 * @param response
	 */
	public void ZhongDianTuijianList(HttpServletRequest request,
			HttpServletResponse response){
		request.setAttribute("currentmenu", "查看微博主");
		request.setAttribute("currentmenuitem", "重点推荐");
		try {
			HostService hostService = new HostServiceImp();
			String currentPage = request.getParameter("currentPage");
			if(currentPage==null||"".equals(currentPage)){
				currentPage = "1";
			}
			//获取游客列表
			List list = hostService.getZhongdianTuijian(Integer.parseInt(currentPage)-1);
			request.setAttribute("hostList", list);
			
			//获取游客列表的总条目数
			int TotalPageNum = CommonUtil.getTotalPageNum(hostService.getZhongdiantuijianSize());
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("totalPageNum", TotalPageNum);
			request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
			logger.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	/**
	 * 收藏夹
	 * @param request
	 * @param response
	 */
	public void FavoriteList(HttpServletRequest request,
			HttpServletResponse response){
		request.setAttribute("currentmenu", "查看微博主");
		request.setAttribute("currentmenuitem", "查看收藏夹");
		try {
			HostService hostService = new HostServiceImp();
			String currentPage = request.getParameter("currentPage");
			if(currentPage==null||"".equals(currentPage)){
				currentPage = "1";
			}
			//获取游客列表
			User user = CommonUtil.getLoginUser(request);
			List list = hostService.getFavoriteList(Integer.parseInt(currentPage)-1,user.getUser_id());
			request.setAttribute("hostList", list);
			
			//获取游客列表的总条目数
			int TotalPageNum = CommonUtil.getTotalPageNum(hostService.getFavoriteSize(user.getUser_id()));
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("totalPageNum", TotalPageNum);
			request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
			logger.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	/**
	 * 历史
	 * @param request
	 * @param response
	 */
	public void HistoryList(HttpServletRequest request,
			HttpServletResponse response){
		request.setAttribute("currentmenu", "查看微博主");
		request.setAttribute("currentmenuitem", "查看历史");
		try {
			HostService hostService = new HostServiceImp();
			String currentPage = request.getParameter("currentPage");
			if(currentPage==null||"".equals(currentPage)){
				currentPage = "1";
			}
			//获取游客列表
			User user = CommonUtil.getLoginUser(request);
			List list = hostService.getHistoryList(Integer.parseInt(currentPage)-1,user.getUser_id());
			request.setAttribute("hostList", list);
			
			//获取游客列表的总条目数
			//User user = CommonUtil.getLoginUser(request);
			int TotalPageNum = CommonUtil.getTotalPageNum(hostService.getHistorySize(user.getUser_id()));
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("totalPageNum", TotalPageNum);
			request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
			logger.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	/**
	 * 黑名单
	 * @param request
	 * @param response
	 */
	public void BlackList(HttpServletRequest request,
			HttpServletResponse response){
		request.setAttribute("currentmenu", "查看微博主");
		request.setAttribute("currentmenuitem", "查看黑名单");
		try {
			HostService hostService = new HostServiceImp();
			String currentPage = request.getParameter("currentPage");
			if(currentPage==null||"".equals(currentPage)){
				currentPage = "1";
			}
			//获取游客列表
			User user = CommonUtil.getLoginUser(request);
			List list = hostService.getBlackList(Integer.parseInt(currentPage)-1,user.getUser_id());
			request.setAttribute("hostList", list);
			
			//获取游客列表的总条目数
			int TotalPageNum = CommonUtil.getTotalPageNum(hostService.getBlackSize(user.getUser_id()));
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("totalPageNum", TotalPageNum);
			request.getRequestDispatcher("/WEB-INF/app/ClientArea_scan.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
			logger.error(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	
	
	
	
	

}
